# frozen_string_literal: true
require 'bundler'
Bundler::GemHelper.install_tasks

require 'rspec/core/rake_task'
desc 'Run resort specs'
RSpec::Core::RakeTask.new

require 'yard'
YARD::Rake::YardocTask.new(:docs) do |t|
  t.files   = ['lib/**/*.rb']
  t.options = ['-m', 'markdown', '--no-private', '-r', 'Readme.md', '--title', 'Resort documentation']
end

site = 'doc'
source_branch = 'master'
deploy_branch = 'gh-pages'

desc 'generate and deploy documentation website to github pages'
multitask :pages do
  puts ">>> Deploying #{deploy_branch} branch to Github Pages <<<"
  require 'git'
  repo = Git.open('.')
  puts "\n>>> Checking out #{deploy_branch} branch <<<\n"
  repo.branch(deploy_branch.to_s).checkout
  (Dir['*'] - [site]).each { |f| rm_rf(f) }
  Dir["#{site}/*"].each { |f| mv(f, './') }
  rm_rf(site)
  puts "\n>>> Moving generated site files <<<\n"
  Dir['**/*'].each { |f| repo.add(f) }
  repo.status.deleted.each { |f, _s| repo.remove(f) }
  puts "\n>>> Commiting: Site updated at #{Time.now.utc} <<<\n"
  message = ENV['MESSAGE'] || "Site updated at #{Time.now.utc}"
  repo.commit(message)
  puts "\n>>> Pushing generated site to #{deploy_branch} branch <<<\n"
  repo.push
  puts "\n>>> Github Pages deploy complete <<<\n"
  repo.branch(source_branch.to_s).checkout
end

task doc: [:docs]

task default: [:spec]
